package org.hc.test;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.nio.IOControl;
import org.apache.http.nio.client.methods.AsyncCharConsumer;
import org.apache.http.nio.client.methods.HttpAsyncMethods;
import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
import org.apache.http.protocol.HttpContext;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

public class QuickStart {

    public static void main(String[] args) throws Exception {
        // 测试get请求
        //testGet();
        // 测试post请求
        testPost();
    }

    /**
     * 测试get请求
     * */
    public static void testGet() throws Exception {
        // 1. 初始化
        CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
        try {
            // 2. 启动客户端
            httpclient.start();

            // 3. 执行Get请求
            final HttpGet request1 = new HttpGet("http://www.apache.org/");
            Future<HttpResponse> future = httpclient.execute(request1, null);
            // 4. 获取响应结果
            HttpResponse response1 = future.get();
            // 5. 打印输出响应结果状态
            System.out.println(request1.getRequestLine() + "->" + response1.getStatusLine());
            // 6. 打印响应结果，比如会返回html页面内容
            System.out.println(convertStreamToString(response1.getEntity().getContent()));
        }finally {
            // 7. 关闭客户端
            httpclient.close();
        }
    }

    public static void testPost() throws Exception{
        // 1. 初始化
        CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
        try {
            // 2. 启动客户端
            httpclient.start();

            // 3. 执行Get请求
            final HttpPost request1 = new HttpPost("http://www.apache.org/");

            // 4. 添加请求参数
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("username", "vip"));
            nvps.add(new BasicNameValuePair("password", "secret"));
            request1.setEntity(new UrlEncodedFormEntity(nvps));

            // 5. 执行请求
            Future<HttpResponse> future = httpclient.execute(request1, null);
            // 6. 获取响应结果
            HttpResponse response1 = future.get();
            // 7. 打印输出响应结果状态
            System.out.println(request1.getRequestLine() + "->" + response1.getStatusLine());
            // 8. 打印响应结果，比如会返回html页面内容
            System.out.println(convertStreamToString(response1.getEntity().getContent()));
        }finally {
            // 9. 关闭客户端
            httpclient.close();
        }
    }

    /**
     * InputStream 转 String
     *
     * @param is 输入流 InputStream
     */
    public static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}
